package com.yuteng.comment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuteng.comment.entity.VideoComment;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.yuteng.comment.mapper.VideoCommentMapper;
import com.yuteng.comment.service.VideoCommentService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 评论表服务接口实现
 *
 * @author 余腾
 * @description 用户喜欢的视频的一些信息
 * @since 2021-10-27 16:30:07
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class VideoCommentServiceImpl implements VideoCommentService {
    private final VideoCommentMapper videoCommentMapper;

    public PageInfo getAll(int pageNum,int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        List<VideoComment> videoComments = videoCommentMapper.selectList(null);
        for (VideoComment videoComment : videoComments) {
            List<VideoComment> list = videoCommentMapper.selectChildrenById(videoComment.getId());
            videoComment.setChildren(list);
        }
        PageInfo pageInfo=new PageInfo(videoComments);
        return pageInfo;
    }

    public List add(VideoComment videoComment) {
        videoCommentMapper.insert(videoComment);
        return null;
    }

    public List updateComment(VideoComment videoComment) {
        videoCommentMapper.updateById(videoComment);
        return null;
    }

    public List deleteById(Long id) {
        List<VideoComment> list = videoCommentMapper.selectChildrenById(id);
        List<Long> l = new ArrayList<>();
        for (VideoComment videoComment : list) {
            l.add(videoComment.getId());
            videoCommentMapper.deleteById(videoComment.getId());
        }
        videoCommentMapper.deleteById(id);
        return null;
    }

    public PageInfo getVideoCommentByUserId(Long userId,int pageNum,int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        List list = videoCommentMapper.selectList(queryWrapper);
        PageInfo pageInfo=new PageInfo(list);
        return pageInfo;
    }
    public PageInfo getVideoCommentByVideId(Long videoId,int pageNum,int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("video_id", videoId);
        List list = videoCommentMapper.selectList(queryWrapper);
        PageInfo pageInfo=new PageInfo(list);
        return pageInfo;
    }
}